﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UsersProvider.Repositories.RelationalRepositories
{
    public class CountryCodesRepository : ICountryCodesRepository
    {
        /// <summary>
        /// Gets the country code from IP number
        /// </summary>
        /// <param name="ipNumber"></param>
        /// <returns></returns>
        public string GetCountryCode(long ipNumber)
        {
            using (UsersModelEntities context = new UsersModelEntities())
            {
                var range = context.CountryIPSet.
                            Where(ip => ip.StartIPNumber <= ipNumber &&
                                                            ipNumber <= ip.EndIPNumber);
                if (range.Count() > 0)
                {
                    return range.First().ShortCode.ToLower();
                }
            }

            return null;
        }
    }
}
